package com.mybatistest.controller;

import com.mybatistest.common.ResponseData;
import com.mybatistest.entity.Mybatis;
import com.mybatistest.service.IMybatisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangrunfu
 * @since 2024-08-30
 */
@Api(tags = "mybatis测试")
@RestController
@Slf4j
@RequestMapping("/mybatistest/mybatis")
@CrossOrigin
@Validated
public class MybatisController {
    @Resource
    IMybatisService mybatisService;
    @Resource
    RedisTemplate<String, Mybatis> redisTemplate;

    /**
     * 保存数据
     * @param mybatis 传进来的瑶保存的数据
     * @return 成功信息以及保存的数据
     */
    @ApiOperation(value = "保存数据接口", notes = "保存数据接口的说明")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<Mybatis> save(@RequestBody @Validated Mybatis mybatis){
        log.info("mybatis: {}", mybatis);
        if (mybatisService.saveData(mybatis)){
            redisTemplate.opsForValue().set(mybatis.getCitizen(), mybatis);
            return ResponseData.success("保存成功", mybatis);
        }
        return ResponseData.fail("保存失败");
    }

    /**
     * 分页查询
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页查询的数据
     */
    @ApiOperation(value = "分页查询", notes = "分页查询的说明")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "int", paramType = "query")})
    @GetMapping("/getPage")
    public ResponseData<List<Mybatis>> getPage(@NotBlank @RequestParam int pageNum, @NotBlank @RequestParam int pageSize){
        log.info("pageNum: {}, pageSize: {}", pageNum, pageSize);
        return ResponseData.success(mybatisService.getPage(pageNum, pageSize));
    }

    /**
     * 根据身份证查询
     * @param citizen 姓名
     * @return 查询到的数据
     */
    @GetMapping("/queryByCitizen")
    @ApiOperation(value = "根据身份证查询", notes = "根据身份证号查询的说明")
    @ApiImplicitParam(name = "citizen", value = "身份证号", required = true, dataType = "String", paramType = "query")
    public ResponseData<Mybatis> queryByCitizen(@RequestParam @NotBlank String citizen){
        if (Boolean.TRUE.equals(redisTemplate.hasKey(citizen))){
            log.info("redis查询");
            return ResponseData.success("redis查询成功", redisTemplate.opsForValue().get(citizen));
        }else {
            log.info("数据库查询");
            if (mybatisService.queryByName(citizen) != null){
                redisTemplate.opsForValue().set(citizen, mybatisService.queryByName(citizen));
            }
            return ResponseData.success("数据库查询成功", mybatisService.queryByName(citizen));
        }
    }

    /**
     * 根据姓名模糊查询多个
     * @param name 姓名
     * @return 查询到的数据
     */
    @GetMapping("/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据name模糊查询多个")
    @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String", paramType = "query")
    public ResponseData<List<Mybatis>> queryById(@RequestParam @NotBlank String name){
        for (Mybatis mybatis : mybatisService.queryByNameList(name)){
            log.info("mybatis: {}", mybatis);
        }
            return ResponseData.success(mybatisService.queryByNameList(name));
    }

    /**
     * 根据身份证模糊查询
     * @param citizen 身份证号
     * @return 查询到的数据
     */
    @GetMapping("/queryByCitizenList")
    @ApiOperation(value = "根据身份证模糊查询", notes = "根据身份证模糊查询的说明")
    @ApiImplicitParam(name = "citizen", value = "身份证号", required = true, dataType = "String", paramType = "query")
    public ResponseData<List<Mybatis>> queryByCitizenList(@RequestParam @NotBlank String citizen){
        return ResponseData.success(mybatisService.queryByCitizenList(citizen));
    }
}
